{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 3 5 7 9]\n"
     ]
    }
   ],
   "source": [
    "# Sorting Arrays\n",
    "# Fast Sorting in NumPy: np.sort and np.argsort\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 9, 0, 7, 3, 5])\n",
    "sort = np.sort(a)\n",
    "print(sort)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 0 4 5 3 1]\n"
     ]
    }
   ],
   "source": [
    "# If you want the sorted index then use np.argsort\n",
    "\n",
    "index = np.argsort(a)\n",
    "print(index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[7 8 7 3 4 8]\n",
      " [6 3 8 8 5 1]\n",
      " [3 3 3 2 6 5]\n",
      " [5 2 5 6 1 5]]\n",
      "-----------------------------------\n",
      "[[3 2 3 2 1 1]\n",
      " [5 3 5 3 4 5]\n",
      " [6 3 7 6 5 5]\n",
      " [7 8 8 8 6 8]]\n",
      "-----------------------------------\n",
      "[[3 4 7 7 8 8]\n",
      " [1 3 5 6 8 8]\n",
      " [2 3 3 3 5 6]\n",
      " [1 2 5 5 5 6]]\n"
     ]
    }
   ],
   "source": [
    "# Sorting along rows or columns\n",
    "\n",
    "X = rand.randint(0, 10, (4, 6))      \n",
    "print(X)\n",
    "print(\"-----------------------------------\")\n",
    "# sort each column of X\n",
    "a = np.sort(X, axis=0)\n",
    "print(a)\n",
    "print(\"-----------------------------------\")\n",
    "# sort each row of X \n",
    "b = np.sort(X, axis =1)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7 2 3 1 6 5 4]\n",
      "------------------------------\n",
      "[2 1 3 4 6 5 7]\n"
     ]
    }
   ],
   "source": [
    "# Partial Sorts: Partitioning\n",
    "x = np.array([7, 2, 3, 1, 6, 5, 4])   \n",
    "print(x)\n",
    "print(\"------------------------------\")\n",
    "a = np.partition(x, 3)\n",
    "print(a)\n",
    "\n",
    "# NumPy provides this in the np.partition function. \n",
    "# np.partition takes an array and a number K; the result is a new array with the smallest K values to the left of the partition,\n",
    "# and the remaining values to the right, in arbitrary order:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 4 7 7 8 8]\n",
      " [1 3 5 8 8 6]\n",
      " [2 3 3 3 6 5]\n",
      " [1 2 5 6 5 5]]\n"
     ]
    }
   ],
   "source": [
    "b = np.partition(X, 2, axis=1)\n",
    "print(b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
